package com.leonzhangxf.springdatastudy.repo.entity;

import com.leonzhangxf.springdatastudy.util.IdGenerator;
import lombok.Getter;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Persistent;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.Table;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * @author leonzhangxf
 */
@Persistent
@Table("student_log")
public class StudentLog {

    @Id
    @Column("id")
    @Getter
    private final String id;

    @Column("student_id")
    @Getter
    private final Long studentId;

    @Column("content")
    @Getter
    private final String content;

    @Column("created")
    @Getter
    private final LocalDateTime created;

    @Column("modified")
    @Getter
    private final LocalDateTime modified;

    public static StudentLog of(Long studentId, String content) {
        return new StudentLog(IdGenerator.generate(), studentId, content,
            LocalDateTime.now(), LocalDateTime.now());
    }

    StudentLog(String id, Long studentId, String content, LocalDateTime created, LocalDateTime modified) {
        this.id = id;
        this.studentId = studentId;
        this.content = content;
        this.created = created;
        this.modified = modified;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        StudentLog that = (StudentLog) o;
        return Objects.equals(id, that.id) &&
            Objects.equals(studentId, that.studentId) &&
            Objects.equals(content, that.content);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, studentId, content);
    }

    @Override
    public String toString() {
        return "StudentLog{" +
            "id='" + id + '\'' +
            ", studentId='" + studentId + '\'' +
            ", content='" + content + '\'' +
            '}';
    }
}
